package com.example.emailcraft.controller;

import com.example.emailcraft.service.impl.CountdownServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Collections;
import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1")
public class CountdownController {

    @Autowired
    private CountdownServiceImpl countdownService;

    // 设置结束时间戳
    @PostMapping("/setCountdown")
    public ResponseEntity<Map<String, Object>> setCountdown(@RequestBody Map<String, Object> request) {
        try {
            String uuid = (String) request.get("uuid");
            Long endTime = ((Number) request.get("endTime")).longValue();
            countdownService.setEndTime(uuid, endTime);
            return ResponseEntity.ok(Collections.singletonMap("success", true));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Collections.singletonMap("success", false));
        }
    }

    // 获取结束时间戳
    @GetMapping("/getCountdown")
    public ResponseEntity<Map<String, Object>> getCountdown(@RequestParam("uuid") String uuid) {
        try {
            Long endTime = countdownService.getEndTime(uuid);
            return ResponseEntity.ok(Collections.singletonMap("endTime", endTime));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Collections.singletonMap("success", false));
        }
    }
}